package com.yz.jdd.receiver;


import android.content.Context;
import android.util.Log;

import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTNotificationMessage;
import com.igexin.sdk.message.GTTransmitMessage;
import com.yz.jdd.base.utils.BaseConfigs;
import com.yz.jdd.base.utils.LogUtils;
import com.yz.jdd.base.utils.PreferencesUtils;

import org.json.JSONObject;

/**
 * 继承 GTIntentService 接收来⾃个推的消息, 所有消息在线程中回调, 如果注册了该服务, 则务必要在 AndroidManifest中声明, 否则⽆法接
 * 受消息<br>
 * * onReceiveMessageData 处理透传消息<br>
 * * onReceiveClientId 接收 cid <br>
 * * onReceiveOnlineState cid 离线上线通知 <br>
 * * onReceiveCommandResult 各种事件处理回执 <br>
 */
public class DemoIntentService extends GTIntentService {
    private static final String TAG = "getui";

    public DemoIntentService() {

    }

    @Override
    public void onReceiveServicePid(Context context, int pid) {
        Log.e(TAG, "onReceiveServicePid：个推" + pid);
    }

    @Override
    public void onReceiveMessageData(Context context, GTTransmitMessage msg) {

        byte[] payload = msg.getPayload();
        String customContent = new String(payload);
        LogUtils.e("=== onReceiveMessageData:个推: " + customContent);
        try {
            JSONObject jsonObject = new JSONObject(customContent);
            Long orderId = jsonObject.getLong("id");
            String content = jsonObject.getString("msg");

//            String payStatus= jsonObject.getString("payStatus");
//            String chineseChars= jsonObject.getString("chineseChars");
            String payStatus="";
            String chineseChars="";
            try {
                payStatus= jsonObject.getString("payStatus");
                chineseChars= jsonObject.getString("ChineseChars");
            }catch (Exception e){
                e.printStackTrace();
            }
            Requst_Update_Utils.sendNotification(context, "" + orderId, content, BaseConfigs.CHANNEL.GETI,payStatus,chineseChars);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onReceiveClientId(Context context, String clientid) {
        Log.e(TAG, "onReceiveClientId ：个推 " + clientid);
        //个推的不判断，防止服务端接收不到getuiPushId,导致无法推送
//        String saved_clientid=PreferencesUtils.getString(context, "getuiPushId", "");
//        if(!saved_clientid.equals(clientid)){
        PreferencesUtils.putString(context, "getuiPushId", clientid);
        Requst_Update_Utils.requstUpdata(context);
//        }

    }

    @Override
    public void onReceiveOnlineState(Context context, boolean online) {
        // Log.e(TAG, "onReceiveOnlineState:个推 " + online);

    }

    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage cmdMessage) {
        // Log.e(TAG, "onReceiveCommandResult:个推 " + cmdMessage.toString());

    }

    @Override
    public void onNotificationMessageArrived(Context context, GTNotificationMessage msg) {
        String content = msg.getContent();
        // Log.e(TAG, "onNotificationMessageArrived: 个推" + content);

    }

    @Override
    public void onNotificationMessageClicked(Context context, GTNotificationMessage msg) {
        // Log.e(TAG, "onNotificationMessageClicked:个推 " + msg.getContent());
    }

}
